package cn.zhoutingrui.pethome.system.menu.service.impl;

import cn.zhoutingrui.pethome.system.menu.domain.Menu;
import cn.zhoutingrui.pethome.system.menu.mapper.MenuMapper;
import cn.zhoutingrui.pethome.system.menu.service.IMenuService;
import cn.zhoutingrui.pethome.base.service.impl.BaseServiceImpl;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ztr
 * @since 2023-02-04
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> tree() {
        List<Menu> all = menuMapper.findAll();
        //获取顶级父类
        List<Menu> tree = all.stream().filter(menu -> menu.getParentId() == null).collect(Collectors.toList());
        tree.forEach(parent-> all
                .stream()
                .filter(menu -> menu.getParentId()!=null)
                .forEach(children->{
                    if (children.getParentId()==parent.getId())
                        parent.getChildren().add(children);
                }));
        return tree;
    }
}
